package com.googlecode.gwtbb.client.only.widget.form.forum;

import com.googlecode.gwtbb.client.only.remote.ClientSession;
import com.googlecode.gwtbb.client.only.remote.forum.PostServiceProxy;
import com.googlecode.gwtbb.client.only.widget.form.FormWidget;
import com.googlecode.gwtbb.client.only.widget.form.entry.FormEntry;
import com.googlecode.gwtbb.client.only.widget.form.entry.SingleLabelFormEntry;
import com.googlecode.gwtbb.client.only.widget.form.entry.SingleTextAreaFormEntry;
import com.googlecode.gwtbb.client.shared.entity.forum.Post;
import com.googlecode.gwtbb.client.shared.remote.forum.CreatePostBean;
import com.googlecode.gwtbb.client.shared.validation.Failure;
import com.googlecode.gwtbb.client.shared.validation.FieldId;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Apr 30, 2008
 */
public class CreateReplyForm extends FormWidget {
	public static final String BODY_LABEL = "bodyLabel";
	private CreatePostBean createPostBean = new CreatePostBean();

	public CreateReplyForm(ClientSession session) {
		super(session);
		init();
	}

	public List<FormEntry> getFormEntries() {
		List<FormEntry> retVal = new ArrayList<FormEntry>(2);
		SingleLabelFormEntry bodyLabel = createSingleLabelFormEntry(BODY_LABEL, 0, cmss.fieldLabelReplyBody(), 0, 0);
		retVal.add(bodyLabel);
		SingleTextAreaFormEntry bodyEntry = createSingleTextAreaFormEntry(Post.BODY, 0, null, 1, 0);
		bodyEntry.getEntryTextArea().setSize("600px", "240px");
		retVal.add(bodyEntry);
		return retVal;
	}

	public String getProcessName() {
		return "createReply";
	}

	public String getProcessSuccess() {
		return cmss.createPostProcessSuccess();
	}

	public String getProcessTitle() {
		return cmss.createPostProcessTitle();
	}

	public void setForumId(Long forumId) {
		createPostBean.setForumId(forumId);
	}

	public void setThreadId(Long threadId) {
		createPostBean.setThreadId(threadId);
	}

	public Map<FieldId, Failure> validate(Map<FieldId, Failure> failures, int index) {
		createPostBean.setBody(getValue(Post.BODY, 0));
		createPostBean.validate(failures, index);
		setValue(Post.BODY, 0, createPostBean.getBody());
		return failures;
	}

	protected void doAction() {
		PostServiceProxy.instance().create(session.getUser().getId(), session.getSessionId(), createPostBean, asyncCallback);
	}
}